#! /bin/sh
: ${DIALOG=dialog}

parse_vars()
{
   IFS=`echo ""`
   NUM=0
   echo $1 | while read LINE
   do
      case $NUM in
         0) echo "SERVER=\"$LINE\"" ;;
         1) echo "USERNAME=\"$LINE\"" ;;
         2) echo "PORTS=\"$LINE\"" ;;
         3) echo "DESCRIPTION=\"$LINE\"" ;;
         4) echo "REPOSITORY=\"$LINE\"" ;;
         5) echo "BUILDS=\"$LINE\"" ;;
         *) ;;
      esac
      NUM=`expr $NUM + 1`
   done
}

to_array()
{
   NAME=$1
   DATA=$2
   NUM=0

   for element in DATA
   do
      echo "&${NAME}[${NUM}]=${element}"
      NUM=`expr $NUM + 1`
   done
}

schedule_jobs()
{
   SERVER="$1"
   USERNAME="$2"
   CLIENTKEY="$3"
   REPOSITORY="$4"
   PORTS="$5"
   GROUPS=`to_array "group" "$6"`
   DESCRIPTION="$7"

   curl --user-agent "rpclient/0.1" --data "username=${USERNAME}&clientkey=${CLIENTKEY}&repository=${REPOSITORY}&portname=${PORTS}${GROUPS}&description=${DESCRIPTION}&addbuild=Add" ${SERVER}/client/addport
}


backtitle="redports client"
returncode=0
while test $returncode != 1 && test $returncode != 250
do
returncode=$?
exec 3>&1
value=`$DIALOG  --title "Rebuild Port" --ok-label "Next" \
    --backtitle "$backtitle" \
    --mixedform "Schedule a rebuild for a list of ports in your repository." 20 50 0 \
        "Server          :" 1 1 "redports.org" 1 20 20 0 2 \
        "Username        :" 2 1 "decke" 2 20 20 0 2 \
        "Ports           :" 3 1 "" 3 20 20 0 0 \
        "Description     :" 4 1 "" 4 20 20 0 0 \
    --backtitle "$backtitle" --ok-label "Next" \
    --menu "Repository" 20 50 10 \
        "svn.redports.org/decke" "" \
        "svn.redports.org/virtualbox" "" \
    --backtitle "$backtitle" --ok-label "Next" \
    --checklist "Builds" 20 50 10 \
        "automatic" "" "on" \
        "10-CURRENT/amd64" "" "off" \
        "10-CURRENT/i386" "" "off" \
        "7.4-RELEASE/amd64" "" "off" \
        "7.4-RELEASE/i386" "" "off" \
        "8.2-RELEASE/amd64" "" "off" \
        "8.2-RELEASE/i386" "" "off" \
        "9-CURRENT/amd64" "" "off" \
        "9-CURRENT/i386" "" "off" \
        "CLANG" "" "off" \
        "GCC-4.5" "" "off" \
        "QATty" "" "off" \
2>&1 1>&3`
returncode=$?
exec 3>&-
show=`echo "$value" |sed -e 's/^//'`

case $returncode in
    1)
        "$DIALOG" \
            --clear \
            --backtitle "$backtitle" \
            --yesno "Really quit?" 10 30
        case $? in
            0)
                break
                ;;
            1)
                returncode=99
                ;;
        esac
        ;;
    0)
        eval `parse_vars "$show"`
        "$DIALOG" \
            --clear \
            --backtitle "$backtitle" --no-collapse --cr-wrap \
            --msgbox "Summary:
=================
Server:     $SERVER
Username:   $USERNAME

Job Descr.: $DESCRIPTION
Repository: $REPOSITORY

Ports:      $PORTS
Builds:     $BUILDS
" 15 50
            ;;
        2)
            echo "Button 2 (Help) pressed."
            exit
            ;;
        3)
            echo "Button 3 (Extra) pressed."
            exit
            ;;
        *)
            echo "Return code was $returncode"
            exit
            ;;
        esac
done

